package com.sfh.appversion.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sfh.appversion.base.exception.ServiceException;
import com.sfh.appversion.base.page.PageResponse;
import com.sfh.appversion.dao.entity.AppInfoDO;
import com.sfh.appversion.dao.entity.AppVersionAssignDO;
import com.sfh.appversion.dao.entity.AppVersionDO;
import com.sfh.appversion.dao.mapper.AppVersionAssignMapper;
import com.sfh.appversion.dao.mapper.AppVersionMapper;
import com.sfh.appversion.dto.req.*;
import com.sfh.appversion.dto.resp.AppVersionCheckRespDTO;
import com.sfh.appversion.server.AppVersionServer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

import static com.sfh.appversion.server.error.AppErrorCodeEnum.*;

/***
 * 应用版本服务
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AppVersionServerImpl implements AppVersionServer {

    private final AppVersionMapper appVersionMapper;
    private final AppVersionAssignMapper appVersionAssignMapper;

    @Override
    public PageResponse<AppVersionDO> queryPage(AppVersionPageReqDTO req) {

        LambdaQueryWrapper<AppVersionDO> queryWrapper = Wrappers.lambdaQuery(AppVersionDO.class)
                .eq(AppVersionDO::getAppId, req.getAppId());
        Page<AppVersionDO> page = new Page<>();
        page.setCurrent(req.getPageNow());
        page.setSize(req.getPageSize());

        Page<AppVersionDO> versionDOPage = appVersionMapper.selectPage(page, queryWrapper);

        return new PageResponse<>(versionDOPage);
    }

    @Override
    public PageResponse<AppVersionAssignDO> queryAssignPersonPage(AppVersionAssignReqDTO req) {

        LambdaQueryWrapper<AppVersionAssignDO> queryWrapper = Wrappers.lambdaQuery(AppVersionAssignDO.class)
                .eq(AppVersionAssignDO::getVersionId, req.getVersionId());

        Page<AppVersionAssignDO> page = new Page<>();
        page.setCurrent(req.getPageNow());
        page.setSize(req.getPageSize());

        Page<AppVersionAssignDO> versionDOPage = appVersionAssignMapper.selectPage(page, queryWrapper);

        return new PageResponse<>(versionDOPage);
    }

    @Override
    public AppVersionCheckRespDTO checkNewVersion(AppVersionCheckReqDTO req) {
        //TODO 应用版本检查请求
        LambdaQueryWrapper<AppVersionDO> queryWrapper = Wrappers.lambdaQuery(AppVersionDO.class)
                .eq(AppVersionDO::getAppId, req.getAppId())
                .eq(AppVersionDO::getVersionStatue, "1")//发布状态正常
                .ge(AppVersionDO::getVersionCode, (req.appVersionCode + 1)) //大于等于
                .orderByAsc(AppVersionDO::getVersion);//按照版本code 降序

        List<AppVersionDO> selectList = appVersionMapper.selectList(queryWrapper);
        if (selectList != null) {
            selectList.forEach(new Consumer<AppVersionDO>() {
                @Override
                public void accept(AppVersionDO appVersionDO) {
                    if ("0".equals(appVersionDO.getVersionEffectType())) {
                        //生效方式 1立即 0定时
                    } else if ("2".equals(appVersionDO.getVersionReleaseType())) {
                        //发布模式： 2指定
                    } else if ("3".equals(appVersionDO.getVersionReleaseType())) {
                        //发布模式： 3定量
                    } else {
                        //发布模式：全量
                    }
                }
            });
        }
        return null;
    }

    @Override
    public boolean add(AppVersionNewReqDTO vo) {

        AppVersionDO appVersionDO = AppVersionDO.builder()
                .appId(vo.getAppId())
                .versionFile(vo.getVersionFile())
                .versionName(vo.getVersionName())
                .versionCode(vo.getVersionCode())
                .versionForce(vo.getVersionForce())
                .versionStatue("1")
                .versionEffectType(vo.getVersionEffectType())
                .versionRegularTimeStart(vo.getVersionRegularTimeStart())
                .versionRegularTimeEnd(vo.getVersionRegularTimeEnd())
                .versionReleaseType(vo.getVersionReleaseType())
                .versionQuantifySize(vo.getVersionQuantifySize())
                .versionContent(vo.getVersionContent())
                .versionPhoneVersions(vo.getVersionPhoneVersion())
                .versionPhoneBrands(vo.getVersionPhoneBrand())
                .versionPhoneModels(vo.getVersionPhoneModel()).build();

        return appVersionMapper.insert(appVersionDO) > 0;
    }

    @Override
    public boolean del(String versionId) {

        //先删除版本指定更新人员
        LambdaQueryWrapper<AppVersionAssignDO> queryWrapper = Wrappers.lambdaQuery(AppVersionAssignDO.class)
                .eq(AppVersionAssignDO::getVersionId, versionId);
        appVersionAssignMapper.delete(queryWrapper);
        //再删除版本
        return appVersionMapper.deleteById(versionId) > 0;
    }

    @Override
    public boolean delAll(String appId) {

        return appVersionMapper.delete(Wrappers.lambdaQuery(AppVersionDO.class)
                .eq(AppVersionDO::getAppId, appId)) > 0;
    }

    @Override
    public boolean editForce(String versionId, String versionForce) {

        AppVersionDO versionDO =   appVersionMapper.selectById(versionId);
        if (versionDO == null){
            throw new ServiceException(APP_VERSION_UNKNOWN_ERROR);
        }
        versionDO.setVersionForce(versionForce);
        return appVersionMapper.updateById(versionDO) > 0;
    }

    @Override
    public boolean editStatue(String versionId,String versionStatue) {
        AppVersionDO versionDO =   appVersionMapper.selectById(versionId);
        if (versionDO == null){
            throw new ServiceException(APP_VERSION_UNKNOWN_ERROR);
        }
        versionDO.setVersionStatue(versionStatue);
        return appVersionMapper.updateById(versionDO) > 0;
    }

    @Override
    public boolean editReleaseType(AppVersionEditReqDTO req) {
        AppVersionDO versionDO =   appVersionMapper.selectById(req.getVersionId());
        if (versionDO == null){
            throw new ServiceException(APP_VERSION_UNKNOWN_ERROR);
        }
        versionDO.setVersionReleaseType(req.getVersionReleaseType());
        versionDO.setVersionQuantifySize(req.getVersionQuantifySize());
        return appVersionMapper.updateById(versionDO) > 0;
    }

    @Override
    public boolean addAssignPerson(AppVersionAssignNewReqDTO req) {

        List<AppVersionAssignDO> assignDOList = new ArrayList<>(req.getAssignPhones().size());
        req.getAssignPhones().forEach(new Consumer<String>() {
            @Override
            public void accept(String phone) {
                AppVersionAssignDO assignDO = AppVersionAssignDO.builder()
                        .assignPhone(phone)
                        .versionId(req.getVersionId())
                        .assignStatue("1").build();
                assignDOList.add(assignDO);
            }
        });
        return appVersionAssignMapper.insertBatchSomeColumn(assignDOList)> 0;
    }

    @Override
    public boolean delAssignPerson(String assignId) {

        return appVersionAssignMapper.deleteById(assignId) > 0;
    }

    @Override
    public boolean editAssignPersonStatue(String assignId, String assignStatue) {

        AppVersionAssignDO assignDO = appVersionAssignMapper.selectById(assignId);
        if (assignDO == null){
            throw new ServiceException(APP_VERSION_PERSION_UNKNOWNT_ERROR);
        }
        assignDO.setAssignStatue(assignStatue);
        return appVersionAssignMapper.updateById(assignDO) > 0;
    }
}
